package com.fengmi.service.impl;

import com.fengmi.dto.PageDTO;
import com.fengmi.entity.MallGoodsBrandEntity;
import com.fengmi.mapper.MallGoodsBrandmapper;
import com.fengmi.service.MallGoodsBrandService;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class MallGoodsBrandServiceImpl implements MallGoodsBrandService {
    @Autowired
    private MallGoodsBrandmapper mallGoodsBrandmapper;

    @Override
    public PageResultVO<MallGoodsBrandEntity> findByPage(PageDTO<MallGoodsBrandEntity> pageDTO) {
        //非空判断
        if (pageDTO == null) {
            return new PageResultVO<>(false, "非法参数");
        }

        //当前页和当前页记录数给默认值
        if (pageDTO.getPageNum() == null || pageDTO.getPageNum() <= 0) {
            pageDTO.setPageNum(1);
        }
        if (pageDTO.getPageSize() == null || pageDTO.getPageSize() <= 0){
            pageDTO.setPageSize(5);
        }

        //开启分页
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());

        //调用mapper层方法
        List<MallGoodsBrandEntity> mallGoodsBrandEntityList = mallGoodsBrandmapper.selectAll(pageDTO.getWhere());

        //获取分页信息
        PageInfo<MallGoodsBrandEntity> pageInfo = new PageInfo<>(mallGoodsBrandEntityList);

        //给控制层返回数据
        PageResultVO<MallGoodsBrandEntity> pageResultVo = new PageResultVO<>(true,"查询成功",pageInfo.getPageNum(), pageDTO.getPageSize(), pageInfo.getTotal(), pageInfo.getList());

        return pageResultVo;
    }


    @Override
    public ResultVO saveInsert(MallGoodsBrandEntity mallGoodsBrand) {
        //非空判断
        if (mallGoodsBrand==null){
            return new ResultVO(false,"参数不合法");
        }
        //调用方法添加到数据库
        int i = mallGoodsBrandmapper.insertBrand(mallGoodsBrand);
        //返回结果
        return new ResultVO(i==1?true:false,i==1?"添加品牌成功":"添加品牌失败");
    }


    @Override
    public ResultVO findOneBrandById(String id) {
        if (StringUtils.isEmpty(id)){
            return new ResultVO(false,"参数不合法");
        }
        //调用mapper层方法查询品牌
        MallGoodsBrandEntity brandById = mallGoodsBrandmapper.getOneBrandById(id);
        //返回
        if (brandById == null){
            return new ResultVO(false,"系统出小差了~~~");
        }

        return new ResultVO(true,"查询成功",brandById);
    }


    @Override
    public ResultVO toggleBrandShowStatus(String id, Integer status) {
        if (StringUtils.isEmpty(id)){
            return new ResultVO(false,"参数不合法");
        }
        int i = mallGoodsBrandmapper.updateBrandStatusById(id, status);

        return new ResultVO(i==1?true:false,i==1?"修改品牌展示状态成功":"修改品牌展示状态失败");
    }


    @Override
    public ResultVO deleteBrandById(String id) {
        if (StringUtils.isEmpty(id)){
            return new ResultVO(false,"参数不合法");
        }
        int i = mallGoodsBrandmapper.delBrandByBrandId(id);

        return new ResultVO(i==1?true:false,i==1?"删除品牌成功":"删除品牌失败");
    }


    @Override
    public ResultVO updateBrandInfo(MallGoodsBrandEntity mallGoodsBrand) {
        if (mallGoodsBrand == null){
            return new ResultVO(false,"参数不合法");
        }
        int i = mallGoodsBrandmapper.updateBrandInfo(mallGoodsBrand);

        return new ResultVO(i==1?true:false,i==1?"修改品牌信息成功":"修改品牌信息失败");
    }


    @Override
    public ResultVO delMoreThanOneBrand(Integer[] ids) {
        if (ids == null || ids.length<=0){
            return new ResultVO(false,"参数不合法");
        }
        int i = mallGoodsBrandmapper.delMore(ids);
        return new ResultVO(i>0?true:false,i>0?"删除成功，共计删除"+i+"条记录":"删除失败");
    }


    @Override
    public ResultVO selectAllBrand() {

        List<MallGoodsBrandEntity> brandList = mallGoodsBrandmapper.findAllBrand();

        return new ResultVO(true,"success",brandList);
    }
}
